package com.oraking.android.hybridapp.dev.korealaw.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.oraking.android.hybridapp.dev.korealaw.R;
import com.oraking.android.hybridapp.dev.korealaw.util.CmmFileUtil;
import com.oraking.android.hybridapp.dev.korealaw.util.CmmHttpUtil;
import com.oraking.android.hybridapp.dev.korealaw.util.CmmStr;
import com.oraking.android.hybridapp.dev.korealaw.util.CmmStrUtil;

public class KoreaLawContent extends Activity {
	Context mContext;
	Intent mIntent;
	
	WebView webView;
	ProgressDialog dial;	// ProgressBar
	
	
	String filePath;		// filePath 얻기
	
	// Handler 관련 상수 Start
	static final int PROGRESS_START = 0;
	static final int PROGRESS_STOP = 1;
	static final int HTML_SHOW = 2;
	static final int DATA_GET = 3;
	static final int ACTIVITY_SWITCH = 4;
	static final int MAKE_HTML = 5;
	// Handler 관련 상수 End
	
	String url;
	String chgUrl;
	
	@Override
	public void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		getResource();							// Resource 가져오기
		getIntentInfo();						// 넘긴데이터 구성
		configueScreen();						// 화면 구성하기
		mHandler.sendEmptyMessage(MAKE_HTML);	// 데이터 요청하기
	}
	
	// Data Communication 관련 Handler
	Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case PROGRESS_START:
				dial.show();
				break;
			case PROGRESS_STOP:
				dial.dismiss();
				break;
			case HTML_SHOW:
				getSavedHTML();
				break;
			case ACTIVITY_SWITCH:
				break;
			case DATA_GET:
				break;
			case MAKE_HTML:
				makeHTML();
				break;
			default:
				break;
			}
		}
		
	};
	
	/**
	 * @title  : 
	 * @location : com.oraking.android.hybridapp.dev.korealaw.activity.KoreaLawCn.configueScreen
	 * @writeDay : 2011. 10. 4. 오전 10:11:18
	 * @Todo  : TODO
	 */
	private void configueScreen() {
		// TODO Auto-generated method stub
		setContentView(R.layout.korealawcontent);
		webView = (WebView) findViewById(R.id.webview);
		        
		// WebView Setting Start
		webView.setWebViewClient(new ProxyWebViewClient());						// WebViewClient 지정
		webView.getSettings().setJavaScriptEnabled(true);						// Javascript 사용시 설정
		webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webView.getSettings().setAllowFileAccess(true);
		webView.getSettings().setLightTouchEnabled(true);
		webView.getSettings().setBuiltInZoomControls(true);
		webView.setHorizontalScrollBarEnabled(false);
		webView.setVerticalScrollbarOverlay(true);								// 웹뷰 오른쪽 스크롤바 공간 없애기 설정
		// WebView Setting End
	}
	
	// 리소스 구성 관련 Method
	private void getResource(){
		mIntent = getIntent();											// Intent 객체 얻기
		mContext = getApplicationContext();								// Content 객체 얻기
        filePath = mContext.getFilesDir().getPath()+"/";				// filePath 얻기
        
        // ProgressDialog 리소스 얻기 Start
		dial = new ProgressDialog(KoreaLawContent.this);
        dial.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dial.setMessage("Wait a monent...");
        // ProgressDialog 리소스 얻기 End
        
	}
	
	// 이전페이지에서 넘긴 값 가져오는 메소드
	private void getIntentInfo(){
		url = mIntent.getStringExtra("url");
		chgUrl = CmmStr.LAWURL+url.replace("/DRF/lawService.do?OC=open&target=law&MST=", "/LSW/lsInfoR.do?lsiSeq=").replace("&type=HTML", "");
	}
	
	
	private void makeHTML(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				
				CmmFileUtil.chkFile(filePath, CmmStr.CONTENT_CSS_PATH, CmmStr.CONTENT_CSS_FILES, KoreaLawContent.this);		// CSS File 복사
				
				CmmFileUtil.writeFile(filePath, CmmStr.HTML_CONTENT_FILE_NAME, CmmStrUtil.getHtmlStartWithCSS(CmmStr.CONTENT_CSS_FILES)+ CmmStr.CSS_ADD_TAG + CmmHttpUtil.fromHtmlToStr(chgUrl));
				mHandler.sendEmptyMessage(HTML_SHOW);
			}
		}).start();
	}
	
	// 저장된 HTML 가져오는 Mehtod
	private void getSavedHTML(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				webView.loadUrl("file://"+filePath+CmmStr.HTML_CONTENT_FILE_NAME);
			}
		}).start();
	}
		
	
	private class ProxyWebViewClient extends WebViewClient {

		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			mHandler.sendEmptyMessage(PROGRESS_STOP);
			super.onPageFinished(view, url);
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// TODO Auto-generated method stub
			super.onPageStarted(view, url, favicon);
			mHandler.sendEmptyMessage(PROGRESS_START);
		}
		
		
	}
}
